//
//  ST_PairsCenterOnlyMyLovePecCell.swift
//  btvideo
//
//  Created by mac on 2020/9/5.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit
import SwiftyJSON
import SwiftyAttributes
import HandyJSON

// 只有我喜欢的特质
class ST_PairsCenterOnlyMyLovePecCell: UITableViewCell {
    
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        self.selectionStyle = .none
    
        contentView.backgroundColor = UIColor.init(hex: 0xACB4C0)
        
        contentView.addSubview(corner_radius_view)
        corner_radius_view.snp.makeConstraints { (make) in
            make.edges.equalToSuperview().inset(UIEdgeInsets.init(top: 3, left: 0, bottom: 0, right: 0))
        }
        
        corner_radius_view.addSubview(matchValueImageView)
        matchValueImageView.snp.makeConstraints { (make) in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(118)
        }
        
        
        contentView.addSubview(heart_image_view)
        heart_image_view.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.width.height.equalTo(58)
            make.top.equalToSuperview().offset(15)
        }
        
        contentView.addSubview(value_label)
        value_label.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.height.equalTo(20)
            make.top.equalToSuperview().offset(83)
        }
        
        contentView.addSubview(myLikeItemView)
        myLikeItemView.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.top.equalTo(matchValueImageView.snp.bottom)
            make.height.equalTo(100)
        }
        
        contentView.addSubview(addTitleLabel)
        
        addTitleLabel.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(15)
            make.height.equalTo(16+20+20)
            make.top.equalTo(myLikeItemView.snp.bottom)
        }
        
        contentView.addSubview(addItemView)
        addItemView.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(15)
            make.right.equalToSuperview().offset(-15)
            make.height.equalTo(70)
            make.top.equalTo(addTitleLabel.snp.bottom)
        }
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    fileprivate lazy var value_label:UILabel = {
       let control = UILabel()
        return control
    }()
    
    
    fileprivate lazy var heart_image_view:UIImageView = {
       let control = UIImageView()
        control.image = ST_ImageUtils.imageName("peculiarity_value_heart_icon")
        return control
    }()
    
    fileprivate lazy var corner_radius_view:UIView = {
        let control = UIView()
        control.backgroundColor = Color.white
        control.cornerRadius = 30
        return control
    }()
    
    fileprivate lazy var matchValueImageView:UIImageView = {
       let control = UIImageView()
        control.contentMode = UIView.ContentMode.scaleAspectFill
        control.image = ST_ImageUtils.imageName("peculiarity_match_value_icon_top")
        control.clipsToBounds = true
        return control
    }()
    
    
    fileprivate lazy var myLikeItemView:PairsCenterPeculiaritySectionView = {
       let control = PairsCenterPeculiaritySectionView()
//        control.backgroundColor = .random
        return control
    }()
    
    fileprivate lazy var addTitleLabel:UILabel = {
       let control = UILabel()
        control.textColor = UIColor.init(hex: 0x333333)
        control.font = UIFont.systemFont(ofSize: 16, weight: UIFont.Weight.semibold)
        return control
    }()
    
    fileprivate lazy var addItemView:OnlyMyLoveAddPecView = {
       let control = OnlyMyLoveAddPecView()
        return control
    }()
    
    
    
    
    var model:ST_PairsCenterOnlyMyLovePecCellModel?{
        didSet{
            
            guard let m = model else { return }
            
            
            value_label.attributedText = m.matchNumberStr
            
            myLikeItemView.snp.updateConstraints { (make) in
                make.height.equalTo(m.myLoveArrayHeight)
            }
            
            myLikeItemView.model_list = m.myLoveArray
            
            myLikeItemView.title = m.genderStr + "有我喜欢的特质"
          
            addTitleLabel.text = "我没有\(m.genderStr)喜欢的特质"
        }
    }
    
}

//MARK: - Model
class ST_PairsCenterOnlyMyLovePecCellModel: NSObject,ST_CellTypeProtocol {
    
    var cellType: ST_PairsCenterCellType = .onlyMyLovePeculiarity
    
    var pairsCellHeight: CGFloat = 100
    
    var myLoveArray:[ST_PairsPeculiarity]
    
    var myLoveArrayHeight = 0.cgFloat
    
    var matchInfo:PairsMatchInfo
    var genderStr:String
//    ex:她有4个你喜欢的特质
    var matchNumberStr = NSAttributedString()
    /**
     * Instantiate the instance using the passed json values to set the properties values
     */
    init(fromJson json: JSON,gender:String){
        
        genderStr = gender
        
        myLoveArray = json["target_have_i_like"].arrayValue.map { (itemJson) -> ST_PairsPeculiarity in
            return ST_PairsPeculiarity.deserialize(from: itemJson.rawString() ?? "") ?? ST_PairsPeculiarity()
        }
        
      
        matchInfo = PairsMatchInfo.init(fromJson: json["match_info"])
        
        super.init()
        
        //她有4个你喜欢的特质
        
        matchNumberStr = "\(gender)有".withAttributes([Attribute.textColor(Color.white),Attribute.font(Font.systemFont(ofSize: 14, weight: UIFont.Weight.semibold))]) + matchInfo.targetHaveILikeCount.string.withAttributes([Attribute.textColor(Color.init(hex: 0xFF1F56)!),Attribute.font(Font.systemFont(ofSize: 14, weight: UIFont.Weight.semibold))]) + "个你喜欢的特质".withAttributes([Attribute.textColor(Color.white),Attribute.font(Font.systemFont(ofSize: 14, weight: UIFont.Weight.semibold))])
        
        
        myLoveArrayHeight = PairsCenterPeculiaritySectionView.getHeight(itemCount: myLoveArray.count)
      
        
         pairsCellHeight = myLoveArrayHeight + 156 + 118
        
    }
    
}

//MARK: - Add View

class OnlyMyLoveAddPecView:UIControl{
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.backgroundColor = UIColor.init(hexString: "#FEC207", transparency: 0.1)
                
        self.cornerRadius = 15
        self.addSubview(titleLabel)
        self.addSubview(imageView)
        self.addSubview(add_btn)
        
        imageView.snp.makeConstraints { (make) in
            make.width.height.equalTo(40)
            make.centerY.equalToSuperview()
            make.left.equalToSuperview().offset(15)
        }
        
        titleLabel.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(15+10+40)
            make.centerY.equalToSuperview()
        }
        
        
        add_btn.snp.makeConstraints { (make) in
            make.width.equalTo(72)
            make.height.equalTo(30)
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(-15)
        }
        
        self.addTarget(self, action: #selector(push_add_pec_handle), for: UIControl.Event.touchUpInside)
    }
    
    @objc fileprivate func push_add_pec_handle(){
        
        
        push_to_push_peculiarity_controller(navigationController: self.parentViewController?.navigationController)
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
//    MARK:- Properties
    fileprivate lazy var titleLabel:UILabel = {
        let control = UILabel.init()
        control.numberOfLines = 0
        control.attributedText = "添加【我的特质】\n让对方更了解你".withAttributes([Attribute.font(Font.systemFont(ofSize: 14, weight: UIFont.Weight.regular)),Attribute.textColor(Color.init(hex: 0x333333)!),Attribute.paragraphStyle(NSParagraphStyle.create_line_spacing_paragraph_style(lineSpacing: 2))])
        return control
    }()
    
    fileprivate lazy var imageView:UIImageView = {
         let control = UIImageView()
        control.image = ST_ImageUtils.imageName("push_pec_mid_icon")
          control.contentMode = .scaleAspectFill
          return control
      }()
    
    fileprivate lazy var add_btn:UIButton = {
            
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.backgroundColor = Color.init(hexString: "#FEC207")
        control.cornerRadius = 30/2.0
        control.setTitleForAllStates("去添加")
        control.isUserInteractionEnabled = false
        control.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.semibold)
        control.setTitleColorForAllStates(UIColor.white)
//        control.addTarget(self, action: #selector(confirm_handle), for: UIControl.Event.touchUpInside)
        return control
    }()
}
